/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Experiment;

import db.DB;
import entities.User;
import helpers.MyPoint;
import helpers.Questionnaires;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arnaldo
 */
public abstract class SimpleExperiment {

    protected int numUsers;
    protected int numSurveys;

    protected User users[];
    protected Questionnaires questionnaires[];

    protected FileWriter fstream;
    protected BufferedWriter out;
    
    protected DB db;

    public SimpleExperiment() {
        
    }

    public void runExperiment(int getU, int getQ) throws IOException {

//        System.out.println("Searching Questionnaires available to users");
        this.setFileOutput("get_questionnaires");

//              System.out.println("User:");
//            System.out.println(users[i]);
//            System.out.println("Questionnaires:");

        Random rand = new Random();
        int i = 0;
        String query = "";

        for(int y = 0; y < getU; y++)
        {
            i = rand.nextInt(users.length);
            query = this.db.getQuestionnaires(users[i].getLocation().lat(), users[i].getLocation().lon());
            out.write(query + ";\n");
        }

        this.out.close();

        this.setFileOutput("get_users");

//            System.out.println("User:");
//            System.out.println(users[i]);
//            System.out.println("Questionnaires:");
        for(int y = 0; y < getQ; y++)
        {
            i = rand.nextInt(questionnaires.length);
            Questionnaires q = questionnaires[i];

            query = this.db.getUsers(q.getGeom());
            out.write(query + ";\n");
        }

        this.out.close();
    }

    public void initData(int numUsers, int numSurveys) throws IOException{

        this.numUsers = numUsers;
        this.numSurveys = numSurveys;

        if(numUsers > 0)
        {
            this.initUserToDB();
            this.out.close();
        }

        
        
        if(numSurveys > 0)
        {
            this.initQuestionnaires();
        }

        this.out.close();
    }

    public void initUserToDB() throws IOException {

        users = new User[numUsers];
        for (int i = 0; i < numUsers; i++) {
            users[i] = new User();
            users[i].setId(i + 1);
        }

//        System.out.println("Starting user insertion");
//
//        long startTime = System.currentTimeMillis();

        this.setFileOutput("insert_users");

        for (int i = 0; i < numUsers; i++) {
            User u = users[i];
            MyPoint loc = u.getLocation();
            
            String query = this.db.insertUser(u.getId(), u.getPassword(), u.getLogin(), u.getEmail(), u.getAge(), loc.lat(), loc.lon());
            out.write(query + ";\n");
        }

        
//        long stopTime = System.currentTimeMillis();
//
//        double runningTime = (double) (stopTime - startTime) / 1000;
//
//        System.out.println("Done with user insertion");
//        System.out.println("Total Time: " + runningTime + " seconds.");
//        System.out.println("Avergae time per user insertion " + runningTime / numUsers);
    }

    public void initQuestionnaires() throws IOException
    {
        questionnaires = new Questionnaires[numSurveys];
        for(int i=0;i<numSurveys;i++){
            questionnaires[i] = new Questionnaires(i);
        }

//        System.out.println("Starting questionnaires insertion");
//
//        long startTime = System.currentTimeMillis();
        this.setFileOutput("insert_questionnaires");

        
        for(int i=0;i<numSurveys;i++){
            Questionnaires q = questionnaires[i];
            String query = this.db.insertQuestionnaire(i, q.getCompanyId(), q.getGeom());
            out.write(query + ";\n");
        }

        
        this.out.close();

//        long stopTime = System.currentTimeMillis();
//
//        double runningTime = (double) (stopTime - startTime) / 1000;
//
//        System.out.println("Done with questionnaires insertion");
//        System.out.println("Total Time: " + runningTime + " seconds.");
//        System.out.println("Avergae time per questionnaires insertion " + runningTime / numSurveys);
    }

    public void setFileOutput(String name)
    {
        try //this is to catch only the exception if the stream is already closed
        {
            if(this.fstream != null)
                this.fstream.close();
        }
        catch(IOException e)
        {

        }

        try
        {
            if(this.out != null)
                this.out.close();
        }
        catch(IOException e)
        {

        }
        
        try
        {
            if(this.db.db_in_use() == DB.db.MySQL){
                this.fstream = new FileWriter("m_f_" + name + ".sql");
            }else{
                this.fstream = new FileWriter("p_f_" + name + ".sql");
            }

            this.out = new BufferedWriter(fstream);

        }
        catch (IOException ex)
        {
            Logger.getLogger(SimpleExperiment.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
